﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

using CqusInfo.RAL.Permissions;
using CqusInfo.CR.SqlHelper;

namespace CqusInfo.DAL.Permissions
{
    public class RoleDAL:Inteface.IRoleDAL
    {
        public Int32 GetCounts(bool isDel)
        {
            return SQLiteHelper.GetCounts("tb_Role", isDel);
        }

        /// <summary>
        /// 得到参数
        /// </summary>
        /// <param name="model">数据集</param>
        /// <returns>parameters</returns>
        private SQLiteParameter[] GetParameters(RoleModel model)
        {
            SQLiteParameter[] parameters ={
                                         new SQLiteParameter("@rId",DbType.String),
                                         new SQLiteParameter("@roleName",DbType.String),
                                         new SQLiteParameter("@description",DbType.String)
                                         };
            parameters[0].Value = Guid.NewGuid().ToString();
            parameters[1].Value = model.c_vc_roleName;
            parameters[2].Value = model.c_vc_description;

            return parameters;
        }

        public Int32 Add(RAL.Permissions.RoleModel model)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("insert into tb_Role values(");
            sqlStr.Append("c_vc_rId=@rId,c_vc_roleName=@roleName,c_vc_description=@description,c_b_isDel=" + false + ")");

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), GetParameters(model));
        }

        public Int32 Update(string rId, RAL.Permissions.RoleModel model)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("update tb_Role set ");
            sqlStr.Append("c_vc_roleName=@functionName,c_vc_description=@description where c_vc_rId=@rId");

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), GetParameters(model));
        }

        public Int32 Delete(string fId, bool isDel, bool trueDel)
        {
            if (trueDel)
                return SQLiteHelper.Delete("tb_Role", "c_vc_rId", fId);
            else
                return SQLiteHelper.Delete("tb_Role", "c_vc_rId", fId, "c_b_isDel", isDel);
        }

        public List<RoleModel> Query(string fId)
        {
            List<RoleModel> roleList = new List<RoleModel>();
            DataSet ds = SQLiteHelper.Query("tb_Role", "c_vc_rId", fId);
            string cacheId = string.Format("RoleModel_{0}", fId);
            roleList = SqlData.GetQueryList<RoleModel>(cacheId, "RoleModel", ds);
            return roleList;
        }

        public List<RoleModel> Query(Int32 page, Int32 takeItem, bool isDel)
        {
            List<RoleModel> roleList = new List<RoleModel>();
            DataSet ds = SQLiteHelper.Query("tb_Role", "c_b_isDel", isDel, page, takeItem);
            string cacheId = string.Format("RoleModel_{0}_{1}_{2}", page, takeItem, isDel);
            roleList = SqlData.GetQueryList<RoleModel>(cacheId, "RoleModel", ds);
            return roleList;
        }
    }
}
